package com.clothe.service;

import java.sql.Connection;
import java.util.List;

import com.clothe.dao.IBuycarDAO;
import com.clothe.dao.impl.BuycarDAOImpl;
import com.clothe.database.DataBase;
import com.clothe.dto.Clothing;
import com.clothe.dto.UserInfo;

public class BuycarService {

	IBuycarDAO buycarDAOImpl = new BuycarDAOImpl();

	public boolean save(Clothing clothing, UserInfo userInfo) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			buycarDAOImpl.setConn(connection);
			flag = buycarDAOImpl.save(clothing, userInfo);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;
	}

	public boolean delete(UserInfo userInfo, Clothing clothing)
			throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			buycarDAOImpl.setConn(connection);
			flag = buycarDAOImpl.delete(userInfo, clothing);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;
	}

	public boolean deleteall(int buyerid) throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			buycarDAOImpl.setConn(connection);
			flag = buycarDAOImpl.deleteall(buyerid);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;
	}

	public boolean update(int buyerid, Clothing clothing, int amount)
			throws Exception {
		Connection connection = null;
		boolean flag = false;
		try {
			connection = DataBase.getConn();
			buycarDAOImpl.setConn(connection);
			flag = buycarDAOImpl.update(buyerid, clothing, amount);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return flag;
	}

	public Clothing get(int buyerid) throws Exception {
		Connection connection = null;
		Clothing clothing = null;
		try {
			connection = DataBase.getConn();
			buycarDAOImpl.setConn(connection);
			clothing = buycarDAOImpl.get(buyerid);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return clothing;
	}

	public Clothing getbyclothingid(int buyerid, int clothingid)
			throws Exception {
		Connection connection = null;
		Clothing clothing = null;
		try {
			connection = DataBase.getConn();
			buycarDAOImpl.setConn(connection);
			clothing = buycarDAOImpl.getbyclothingid(buyerid, clothingid);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return clothing;
	}

	public List<Clothing> list(int pageSize, int pageNo, int buyerid)
			throws Exception {
		Connection connection = null;
		List<Clothing> clothinglist = null;
		try {
			connection = DataBase.getConn();
			buycarDAOImpl.setConn(connection);
			clothinglist = buycarDAOImpl.list(pageSize, pageNo, buyerid);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return clothinglist;
	}
	
	public List<Clothing> buycarlist(int buyerid)
			throws Exception {
		Connection connection = null;
		List<Clothing> buycarlist = null;
		try {
			connection = DataBase.getConn();
			buycarDAOImpl.setConn(connection);
			buycarlist = buycarDAOImpl.buycarlist(buyerid);
			DataBase.commit();

		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {
			DataBase.releaseConnection(connection);
		}
		return buycarlist;
	}

	public int getCount(int buyerid) throws Exception {
		int totalpage = 0;
		Connection connection = null;
		try {
			connection = DataBase.getConn();
			buycarDAOImpl.setConn(connection);
			totalpage = buycarDAOImpl.getCount(buyerid);
		} catch (Exception e) {
			DataBase.rollback();
			e.printStackTrace();
			throw e;
		} finally {

			DataBase.releaseConnection(connection);
		}
		return totalpage;

	}

}
